package wysiwyg.tools
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	import mx.core.UIComponent;
	
	import wysiwyg.controls.ColorControls;
	import wysiwyg.layers.Layer;
	import wysiwyg.model.Model;
	import wysiwyg.model.ModelLocator;
	import wysiwyg.objectHandles.HandleBase;
	import wysiwyg.shapes.ShapeBase;

	public class ToolBase implements ITool
	{
		public var label:String;
		[Bindable]
		private var theModel:Model=ModelLocator.getInstance().theModel;
		[Bindable]
		private var layersArray:ArrayCollection=theModel.layersArray;
		public var colorControls:ColorControls;
		
		protected var startPoint:Point;
		protected var endPoint:Point;
		protected var currentPoint:Point;
		protected var currentLayer:Layer;
		protected var shape:ShapeBase;
		protected var handles:HandleBase;
		
		protected var selectionSprite:UIComponent;
		public function ToolBase()
		{
			selectionSprite=new UIComponent();
		}

		public function mouseDownHandler(event:MouseEvent):void
		{
			startPoint=null;
			currentPoint=null;
			endPoint=null;
			selectionSprite.graphics.clear();
			theModel.workspace.addChild(selectionSprite);
			startPoint=new Point(theModel.workspace.mouseX, theModel.workspace.mouseY);
		}
		
		public function mouseUpHandler(event:MouseEvent):void
		{
			if(event.eventPhase==2)
				return;
			theModel.workspace.removeChild(selectionSprite);
			endPoint=new Point(theModel.workspace.mouseX,theModel.workspace.mouseY);
			var right:Number=Math.max(endPoint.x,startPoint.x);
			var left:Number=Math.min(endPoint.x,startPoint.x);
			var top:Number=Math.min(endPoint.y,startPoint.y);
			var bottom:Number=Math.max(endPoint.y,startPoint.y);
			
			
			if(theModel.selectedLayers&&theModel.selectedLayers.length>0)
				currentLayer=theModel.selectedLayers.getItemAt(0) as Layer;
			else
			{
				currentLayer = new Layer();
				currentLayer.percentWidth=100;
				currentLayer.percentHeight=100;
				layersArray.addItemAt(currentLayer,0)
				theModel.workspace.addChild(currentLayer);
			}
			/* shape.x = left;
			shape.y = top;
			shape.width = right - left;
			shape.height = bottom - top;
			shape.color = colorControls.color.selectedColor;
			currentLayer.addChild(shape); */
			handles.x = left;
			handles.y = top;
			handles.width = right - left;
			handles.height = bottom - top;
			handles.shape.color = colorControls.color.selectedColor;
			currentLayer.addChild(shape);
			
			shape.select();
		}
		
		public function mouseMoveHandler(event:MouseEvent):void
		{
			currentPoint=new Point(theModel.workspace.mouseX, theModel.workspace.mouseY);
			if(startPoint)
			{
				selectionSprite.graphics.clear();
				selectionSprite.graphics.beginFill(colorControls.color.selectedColor, .2);
				selectionSprite.graphics.drawRect(startPoint.x,startPoint.y,currentPoint.x-startPoint.x,currentPoint.y-startPoint.y);
				selectionSprite.graphics.endFill();
			}
		}
		
	}
}